from random import Random
import re

class Dice():

  def __init__(self, dices):
    self.dices = dices
    self.dice = Random()
    if re.match(r"(\d+)([kKdD])(\d+)", self.dices):
      m = re.match(r"(\d+)([kKdD])(\d+)", self.dices)
      self.multipler = int(m.group(1))
      self.sides = int(m.group(3))
    elif re.match(r"([kKdD])(\d+)", self.dices):
      m = re.match(r"([kKdD])(\d+)", self.dices)
      self.multipler = 1
      self.sides = int(m.group(2))
    else:
      print "Wrong dice format \"%s\"" % (dices)
      exit(1)
      
  def throw(self):
    result = 0
    self.results = []
    for x in range(self.multipler):
      i = self.dice.randint(1, self.sides)
      result += i
      self.results.append(i)
    return self.results

  def getSum(self):
    if len(self.results) == 0:
      self.throw()
    sum = 0
    for x in self.results:
      sum += x
    return sum


def dice(sides):
  return Dice(sides).throw()

def d100():
  return dice('d100')[0]

def d20():
  return dice('d20')[0]

def d10():
  return dice('d10')[0]
  
def d8():
    return dice('d8')[0]

def d6():
    return dice('d6')[0]

def d4():
    return dice('d4')[0]

def d3():
    return dice('d3')[0]
  

if __name__ == '__main__':
  d = Dice('d3')
  for x in range(10):
      print d.throw()[0] - 2
  print d.getSum()
